﻿#pragma once


#include "RBTree.h"


namespace Set_Map
{
	template<class K, class V>
	class map
	{
	public:
		struct KeyOfT
		{
			const K& operator()(const pair<K, V>& kv)
			{
				return kv.first;
			}
		};

		typedef typename RBTree<K, pair<const K, V>, KeyOfT>::iterator iterator;
		typedef typename RBTree<K, const pair<const K, V>, KeyOfT>::const_iterator const_iterator;

		iterator begin()
		{
			return _t.begin();
		}

		iterator end()
		{
			return _t.end();
		}

		iterator find(const K& key)
		{
			return _t.Find(key);
		}

		pair<iterator, bool> insert(const pair<K, V>& kv)
		{
			return _t.Insert(kv);
		}

		V& operator[](const K& key)
		{
			pair<iterator, bool> ret = insert(make_pair(key, V()));
			return ret.first->second;
			//pair<iterator, bool> ret = insert(key);
			//return ret.first->second;// ?
		}

	private:
		RBTree<K, pair<const K, V>, KeyOfT> _t;
	};

	void test_map2()
	{
		string arr[] = { "苹果", "西瓜", "苹果", "西瓜", "苹果", "苹果", "西瓜",
	"苹果", "香蕉", "苹果", "西瓜", "香蕉", "草莓" };
		map<string, int> countMap;
		for (auto& e : arr)
		{
			/*if (e == "ݮ")
			{
				int i = 0;
			}*/

			countMap[e]++;
		}

		for (auto& kv : countMap)
		{
			cout << kv.first << ":" << kv.second << endl;
		}
		cout << endl;
	}

}